package com.xty.itgroup.tech05tytex.Util;

import com.alibaba.fastjson.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class DataUtil {
    private static String s="abcdefghijklmnopqrstuvwxyz";
    private static String number="0123456789";
    private static String number_string="abcdef0g1h2i3j4k5l6m7n8o9pqrstuvwxyz";
    private static String capital_s="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static String [] start_OF_MONTH= {"","01-01","02-01","03-01","04-01","05-01","06-01","07-01","08-01","09-01","10-01","11-01","12-01"};
    public static String [] END_OF_MONTH= {"","01-31","02-28","03-31","04-30","05-31","06-30","07-31","08-31","09-30","10-31","11-30","12-31"};
    /**
     * 生成随机字符串
     *
     * @param stringLength:生成的字符串长度
     * @return
     */
    public static String getRandomString(int stringLength,String data) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < stringLength; i++) {
            int index = (int) Math.floor(Math.random() * data.length());//向下取整0-25
            sb.append(data.charAt(index));
        }
        return sb.toString();
    }

    public static String getRandom(int stringLength,int data_type) {
        if (data_type==1){//1-字母
            return getRandomString(stringLength,s)  ;
        }else if(data_type==2){//2-数字
            return getRandomString(stringLength,number);
        }else if(data_type==3){//3-字母和数字
            return getRandomString(stringLength,number_string);
        }else if(data_type==4){//3-字母和数字
            return getRandomString(stringLength,capital_s);
        }
        return null;
    }
    public static JSONObject packageJSON(int currentPage, int pageSize, int totalCount, int pageCount, int currentCount){
        JSONObject res= new JSONObject();
        res.put("currentCount",currentCount);
        res.put("totalCount",totalCount);
        res.put("pageCount",pageCount);
        res.put("pageSize",pageSize);
        res.put("currentPage",currentPage);
        return  res;
    }
    //时间取年月日
    public static JSONObject getYearMonthDay(String time,String date_format){
        if ((time == null) || (time.equals(""))) {
            return null;
        }
        Date date = null;
        DateFormat dateFormat = new SimpleDateFormat(date_format);
        String d="";
        try {
            date = new Date(dateFormat.parse(time).getTime());
            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            d=dateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        JSONObject res= new JSONObject();
        res.put("year",Integer.parseInt(d.substring(0, 4)));
        res.put("month",Integer.parseInt(d.substring(5, 7)));
        res.put("day",Integer.parseInt(d.substring(8, 10)));
        res.put("hour",Integer.parseInt(d.substring(11, 13)));
        res.put("minute",Integer.parseInt(d.substring(14, 16)));
        res.put("second",Integer.parseInt(d.substring(17, 19)));
        return res;
    }
    //判断计算记录
    public static int checkCalcultionFlag(Map<String, Integer> map,int month){
        switch (month){
            case 1:
               return map.get("month1");
            case 2:
                return map.get("month2");
            case 3:
                return map.get("month3");
            case 4:
                return map.get("month4");
            case 5:
                return map.get("month5");
            case 6:
                return map.get("month6");
            case 7:
                return map.get("month7");
            case 8:
                return map.get("month8");
            case 9:
                return map.get("month9");
            case 10:
                return map.get("month10");
            case 11:
                return map.get("month11");
            case 12:
                return map.get("month12");
            default:
                return 0;

        }
    }
}
